package com.yzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yzx.constants.Constants;
import com.yzx.domain.SysDictData;
import com.yzx.mapper.SysDictDataMapper;
import com.yzx.vo.DataGridView;
import dto.DictTypeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzx.mapper.SysDictTypeMapper;
import com.yzx.domain.SysDictType;
import com.yzx.service.SysDictTypeService;
@Service
public class SysDictTypeServiceImpl implements SysDictTypeService{

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public DataGridView listPage(DictTypeDto dictTypeDto) {
        QueryWrapper<SysDictType> qw=new QueryWrapper<>();
        Page<SysDictType> page=new Page<>(dictTypeDto.getPageNum(),dictTypeDto.getPageSize());
        qw.like(StringUtils.isNotBlank(dictTypeDto.getDictName()), SysDictType.COL_DICT_NAME,dictTypeDto.getDictName());
        qw.like(StringUtils.isNotBlank(dictTypeDto.getDictType()), SysDictType.COL_DICT_TYPE,dictTypeDto.getDictType());
        qw.eq(StringUtils.isNotBlank(dictTypeDto.getStatus()), SysDictType.COL_STATUS,dictTypeDto.getStatus());
        qw.ge(null!=dictTypeDto.getBeginTime(), SysDictType.COL_CREATE_TIME,dictTypeDto.getBeginTime());
        qw.le(null!=dictTypeDto.getEndTime(), SysDictType.COL_CREATE_TIME,dictTypeDto.getEndTime());
        Page<SysDictType> sysDictTypePage = this.sysDictTypeMapper.selectPage(page, qw);
        return new DataGridView(sysDictTypePage.getTotal(),sysDictTypePage.getRecords());
    }

    @Override
    public DataGridView list() {
        QueryWrapper<SysDictType> qw=new QueryWrapper<>();
        //只查可用的
        qw.eq(SysDictType.COL_STATUS, Constants.STATUS_TRUE);
        return new DataGridView(null,this.sysDictTypeMapper.selectList(qw));
    }

    @Override
    public Boolean checkDictTypeUnique(Long dictId, String dictType) {
        dictId = (dictId == null) ? -1L : dictId;
        QueryWrapper<SysDictType> qw=new QueryWrapper<>();
        qw.eq(SysDictType.COL_DICT_TYPE, dictType);
        SysDictType sysDictType = this.sysDictTypeMapper.selectOne(qw);
        if(null!=sysDictType &&dictId.longValue()!=sysDictType.getDictId().longValue()){
            return true; //说明不存在
        }
        return false; //说明存在
    }

    @Override
    public int insert(DictTypeDto dictTypeDto) {
        SysDictType dictType = new SysDictType();
        BeanUtil.copyProperties(dictTypeDto, dictType);
        dictType.setCreateTime(DateUtil.date());
        dictType.setCreateBy(dictTypeDto.getSimpleUser().getUserName());
        return this.sysDictTypeMapper.insert(dictType);
    }

    @Override
    public int update(DictTypeDto dictTypeDto) {
        SysDictType dictType = new SysDictType();
        BeanUtil.copyProperties(dictTypeDto, dictType);
        dictType.setUpdateBy(dictTypeDto.getSimpleUser().getUserName());
        return this.sysDictTypeMapper.updateById(dictType);
    }

    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        List<Long> ids = Arrays.asList(dictIds);
        if(null!=ids&&ids.size()>0){
            return this.sysDictTypeMapper.deleteBatchIds(ids);
        }else{
            return -1;
        }

    }

    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return this.sysDictTypeMapper.selectById(dictId);
    }

    @Override
    public void dictCacheAsync() {
        ValueOperations<String,String> operations = redisTemplate.opsForValue();
        QueryWrapper<SysDictType> qw=new QueryWrapper<>();
        qw.eq(SysDictType.COL_STATUS, Constants.STATUS_TRUE);
        List<SysDictType> sysDictTypes = this.sysDictTypeMapper.selectList(qw);
        for (SysDictType sysDictType : sysDictTypes) {
            QueryWrapper<SysDictData> qwq=new QueryWrapper<>();
            qwq.eq(SysDictData.COL_DICT_TYPE, sysDictType.getDictType());
            qwq.eq(SysDictData.COL_STATUS, Constants.STATUS_TRUE);
            List<SysDictData> sysDictData = this.sysDictDataMapper.selectList(qwq);
            operations.set(Constants.DICT_REDIS_PROFIX+sysDictType.getDictType(), JSON.toJSONString(sysDictData));
        }
    }
}
